/**
 * 
 */
package com.jysz.certificate.web.action;

import java.io.InputStream;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.service.system.UserService;
import com.jysz.webcore.utils.RandomCodeMaker;

/**
 * @author kacarich email:javaengine@outlook.com
 *
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/")
public class LogonAction extends AbstractBaseAction {
	private InputStream inputStream;

	private User userInfo;
	
	@Resource
	private UserService userService;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	@Action("toPassword")
	public String toPassword(){
		setRedirectUrl("indexBody.do");
		return "password";
	}
	
	@Action("updatePassword")
	public String updatePassword(){
		try{
			User user = userService.find(userInfo.getId());
			user.setPassword(userInfo.getPassword());
			userService.merge(user);
			setSuccessPopMessage("密码修改成功。");
		}catch(Exception e){
			setSuccessPopMessage("密码修改失败");
		}
		return SHOW_MSG_PAGE;
	}
	private String remember;

	@Action("loginPage")
	public String loginPage() {
		return "loginPage";
	}
	
	@Action("login")
	public String login() {
		userInfo = userService.findAuthorizedUser(userInfo);
		if(userInfo != null){
			this.setCurrentSession(userInfo);
			setRedirectUrl("index.do");
			Cookie cookie = new Cookie("cnsys.user", userInfo.getName());
			cookie.setMaxAge(60*60*24*30);
			this.getResponse().addCookie(cookie);
			cookie = new Cookie("cnsys.remember", remember);
			cookie.setMaxAge(60*60*24*30);
			this.getResponse().addCookie(cookie);
			if(remember!= null){
				cookie = new Cookie("cnsys.password", userInfo.getPassword());
				cookie.setMaxAge(60*60*24*30);
				this.getResponse().addCookie(cookie);
			}else{
				cookie = new Cookie("cnsys.password", null);
				cookie.setMaxAge(60*60*24*30);
				this.getResponse().addCookie(cookie);
			}
//			this.setRedirectUrl("mytask/index.do");
			this.setSuccessPopMessage("用户登录成功！");
		}else{
			this.setFailPopMessage("用户登录登陆失败,用户名或者密码错误。！");
		}
		
		
		return SHOW_MSG_PAGE;
	}
 
	@Action("ajaxJsonLogin")
	public String ajaxJsonLogin() {
		return JSON_PAGE;
	}
	
	@Action("logout")
	public String logout() {
		getSession().invalidate();
		return LOGIN_PAGE;
	}
	
	@Action(AUTH_CODE)
	public String authCode() {
		RandomCodeMaker authCode = RandomCodeMaker.getInstance();
		setInputStream(authCode.getImage());// 取得带有随机字符串的图片
		setSessionAttribute(AUTH_CODE, authCode.getString());// 取得随机字符串放入HttpSession
		return AUTH_CODE;
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	@Action("index")
	public String index() {
		
		return INDEX;
	}
	
	@Action("indexBody")
	public String indexBody() {
		
		return "welcome";
	}

	public User getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(User userInfo) {
		this.userInfo = userInfo;
	}

	public String getRemember() {
		return remember;
	}

	public void setRemember(String remember) {
		this.remember = remember;
	}
	
	

}
